/**
 * 
 */
package furniture.render3d;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import org.eclipse.swt.graphics.RGB;

import furniture.items3d.model.tessellation.ItemSet;


/**
 * @author Grill Balazs (balage.g@gmail.com)
 *
 */
public class Line3d implements Polygon3d {

	protected Point3d p1;
	
	protected Point3d p2;
	
	public RGB color = new RGB(0, 0, 0); 
	
	public Line3d(Point3d p1, Point3d p2){
		this.p1 = new Point3d(p1);
		this.p2 = new Point3d(p2);
	}
	
	public Line3d(Point3d p1,Point3d p2,RGB c){
		this(p1,p2);
		this.color = c;
	}
	
	public Point3d center() {
		Point3d p = new Point3d(p1.x+p2.x,p1.y+p2.y,p1.z+p2.z);
		p.scale(0.5);
		return p;
	}

	public Polygon3d copy() {
		return new Line3d(p1,p2,color);
	}

	public boolean fill() {
		return false;
	}

	public RGB getColor() {
		return color;
	}

	public RGB getPaint() {
		return color;
	}

	public List<Point3d> getPoints() {
		List<Point3d> l = new ArrayList<Point3d>();
		l.add(p1);
		l.add(p2);
		return l;
	}

	public void setPoints(List<Point3d> points) {
		p1 = new Point3d(points.get(0));
		p2 = new Point3d(points.get(1));
	}

	public void translate(Vector3d vector) {
		p1.add(vector);
		p2.add(vector);		
	}

	public int compareTo(Polygon3d o) {
		Double a = new Vector3d(this.center()).length();
		Double b = new Vector3d(o.center()).length();
		return a.compareTo(b);
	}

	public Collection<Polygon3d> getTriangles() {
		List<Polygon3d> l = new ArrayList<Polygon3d>();
		l.add(this);
		return l;
	}
	
	public TriangleSet split(int level){
		if (level == 0) return this;
		ItemSet result = new ItemSet();
		
		Point3d p = center();
		result.addSet(new Line3d(p1,p,color));
		result.addSet(new Line3d(p,p2,color));
		
		return result;
	}

	@Override
	public String toString() {
		return p1.toString() + " -> " + p2.toString();
	}
	
}
